import { _decorator, BoxCollider2D, Collider2D, Component, instantiate, JsonAsset, Node, Prefab, Sprite, SpriteFrame, UITransform, Vec3 } from 'cc';
import { ModuleBase } from './ModuleBase';
import { resMgr } from './Res/ResMgr';
import { JsonMgr, PrefabMgr, SpriteFrameMgr } from './Res/SpecialResMgr';
import { Bullet } from './Bullet';
import { BulletType } from './Configs';
const { ccclass, property } = _decorator;
@ccclass('BulletLayers')
export class BulletLayers extends ModuleBase {
    private _bulletPre: Prefab = null;
    //将子弹的脚本存入数组中，方便后续调用脚本中子弹移动的函数
    private _bulletArr: Bullet[] = [];
    get bulletArr() {
        return this._bulletArr;
    }
    onInit(...rest: any[]): void {

        this._bulletPre = resMgr.getMgr<PrefabMgr>("PrefabMgr").getRes<Prefab>("Bullet");
    }
    addBullet(pos: Vec3, dir: Vec3, bulletType: BulletType, roleType: string) {
        let bulletNode = instantiate(this._bulletPre);
        bulletNode.parent = this.node;
        bulletNode.setPosition(pos);
        let bulletName: string = "";
        let jsonData = resMgr.getMgr<JsonMgr>("JsonMgr").getRes<JsonAsset>("DataMgr").json;
        for (let i = 0; i < jsonData.length; i++) {
            if (roleType == jsonData[i].role) {
                bulletName = jsonData[i].bulletType;
            }
        }
        let sprite = bulletNode.getComponent(Sprite);
        sprite.spriteFrame = resMgr.getMgr<SpriteFrameMgr>("SpriteFrameMgr").getRes<SpriteFrame>(bulletName);
        let boxCollider2D = bulletNode.getComponent(BoxCollider2D);
        let uitransform = bulletNode.getComponent(UITransform);
        boxCollider2D.size = uitransform.contentSize;
        let bulletTs = bulletNode.getComponent(Bullet)
        this._bulletArr.push(bulletTs);
        //设置子弹的方向
        bulletTs.oninit(dir, bulletType);
    }
    onUpdate(dt: number, ...rest: any[]): void {
        for (let i = 0; i < this._bulletArr.length; i++) {
            this._bulletArr[i].onUpdate(dt);
        }
    }
}


